/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file creates the dataset used in our analyses and generates all the
results presented in the main text of our paper. It executes the following
STATA do files and r scripts located within the "code" folder:
	1. data_prep/prepare_wrg_data.do
	2. summary_stats/descriptive_stats_plots.do
	3. empirical_analysis/twfe_att_es.do
	4. empirical_analysis/cs_estimates.R
	5. empirical_analysis/figure3.do
	6. empirical_analysis/figure4and5.do
*/

*Define directories and set working directory
/* NOTE - To replicate our results, you need to change the root directory address in the next line */
global dr_root = "\replication materials"
global dr_code = "${dr_root}\code"
global dr_data = "${dr_root}\data"
global dr_output = "${dr_root}\outputs"
global dr_output_main = "${dr_root}\outputs\main_text"
global dr_output_app = "${dr_root}\outputs\appendices"
global dr_output_log = "${dr_root}\outputs\logs"
global dr_temp = "${dr_root}\data\intermediate"
cd "${dr_root}"

/* Run do file aggregating WIMAS records to water right group level */
do "${dr_code}/data_prep/prepare_wrg_data.do"

/* Run do file creating Table 1, Figure 1, and Figure 2 in the main text */
do "${dr_code}/summary_stats/descriptive_stats_plots.do"

/* Run do file estimating Average and Dynamic Treatment Effects for both changes in 
	irrigation technology using TWFE. This do file also outputs the 
	sub-sample statistics in Table 2. */
do "${dr_code}/empirical_analysis/twfe_att_es.do"

/* Run r script which estimates Average and Dynamic Treatment Effects for both
	changes in irrigation technology using Callaway and Sant'Anna (2020) estimator */
	*Create log file (each stata .do file also creates an individual log file)
		log using "$dr_output_log\cs_estimates", replace
rscript using "${dr_code}/empirical_analysis/cs_estimates.R", ///
	args("${dr_root}") rversion(3.6)
	*Close log
	log close

/* Run do file that combines TWFE and Callaway and Sant'Anna Average Treatment 
	Effect estimates, and then graphs them to create Figure 3. */
do "${dr_code}/empirical_analysis/figure3.do"

/* Run do file that combines TWFE and Callaway and Sant'Anna Dynamic Treatment 
	Effect estimates, and then graphs them to create Figures 4 and 5. */
do "${dr_code}/empirical_analysis/figure4and5.do"

